module ModuleQERRORS

	use GLOBALS
	use ModuleAGGREGATES_QTR
	use Functions_wa_TR

contains

	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!         Compute Excess Demand                                   !!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    function ED_TR(xrin_TR,xmtin_TR)  result(ED_out)
	
	    implicit none
    
        ! Local variable declarations
	    real(8), dimension(T_TR), intent(in)  :: xrin_TR, xmtin_TR  ! inputs: transformed r and mt
	    real(8), dimension(2*T_TR)            :: ED_out             ! output: deviations in eqm conditions
	    real(8), dimension(T_TR)              :: amtin_TR, arin_TR  ! for transforming r and mt back
	
        ! recover r, mt, wge in levels
        arin_TR     = exp(xrin_TR)/(1.0D0 + exp(xrin_TR))
	    r_TR        = arin_TR*rlb + (1.0D0-arin_TR)*rub
	
	    amtin_TR   = exp(xmtin_TR)/(1.0D0 + exp(xmtin_TR))
	    mt_TR      = amtin_TR*mtlb + (1.0D0-amtin_TR)*mtub

        wge_TR = (1.0D0-alpha)/(r_TR+delta)
        wge_TR = alpha*(wge_TR**((1d0-alpha)/alpha))

        ! Compute transition
        call Model_VF_TR
        call ComputeMEASURE_TR
        call ComputeAGGREGATES_TR

        ! Store eqm conditions
        ED_out(1:T_TR)          = asset_TR
        ED_out(T_TR+1:2*T_TR)   = BC_TR
    
	end function ED_TR
	
	
	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!       				Compute VF       					  !!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    subroutine Model_VF_TR
    
        implicit none
        
        ! Local variable declarations
        integer :: tt, is
    
        real(8), dimension(NS) :: xra_hmax
        real(8), dimension(NS) :: Vnew

        ! initialize last period --> value and policies
        V_TR(:,T_TR)       = V_LR
        h_pol_TR(:,T_TR)   = h_pol_LR
        c_pol_TR(:,T_TR)   = c_pol_LR
        a_pol_TR(:,T_TR)   = a_pol_LR
        
        ! iterate backwards
        do tt = T_TR-1,1,-1
            
            ! prepare continuation value
            call ComputeVe_TR(tt+1)
            
            ! max asset choice
            xra_hmax = min(amax,S(:,1)*(1d0+r_TR(tt)) + wge_TR(tt)*S(:,2)*hmax - TAX_TR(wge_TR(tt)*S(:,2)*hmax,r_TR(tt)*S(:,1),tt))

            ! Find optimal a
            ! NESTED GOLDEN
            !$OMP PARALLEL DO DEFAULT(SHARED)
            do is = 1, NS
                a_pol_TR(is,tt) = SolveBell_wa_hopt_sc_TR(amin,xra_hmax(is),is,tt)
            enddo
            !$OMP END PARALLEL DO

            ! value associated with optimal choice given by golden
            !$OMP PARALLEL DO DEFAULT(SHARED)
            do is = 1, NS
                Vnew(is)  = VF_eval_wa_hopt_sc_TR(a_pol_TR(is,tt),is,1,tt)  
            enddo
            !$OMP END PARALLEL DO

            ! store value function for this period
            V_TR(:,tt) = Vnew

        enddo ! end loop tt
    
    end subroutine Model_VF_TR

end module ModuleQERRORS              
